package my.jwds.p440;

public class FindKthNumber {

    public static void main(String[] args) {

        FindKthNumber obj = new FindKthNumber();
        long start = System.currentTimeMillis();
//        for (int i = 1; i <= 103; i++) {
//            System.out.println(obj.findKthNumber(103,i));
//        }

        System.out.println(obj.findKthNumber(681692778,351251360));

        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }

    public int findKthNumber(int n, int k) {
        int res = 0;
        for (int i = 0; i < 10; i++) {
            int count = getCount(n,res * 10 + i);
            if (k > count){
                k -= count;
            }else{
                res = res * 10 + i;
                i = -1;
                k--;
                if (k <= 0)break;
            }
        }
        return res;

    }


    int getCount(int n,int num){
        if (num == 0)return 0;
        int res = 0;
        long c = 1,temp = 0;
        while(n >= temp){
            if (temp+c-1 <= n){
                res += c;
            }else{
                res += n-temp+1;
            }
            c *=10;
            temp = num*c;
        }
        return res;
    }
}